/**
  ******************************************************************************
  * File Name          : route_task.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "bsp.h"
#include "tskcfg.h"
#include "route.h"
#include "network.h"

TaskHandle_t xRoute_422_Thd = NULL;
StaticTask_t xRoute_422_Tcb;
StackType_t  xRoute_422_Stk[ROUTE_422_STACK_SIZE];

Route_PayloadType Route_RxLink422;
static Route_PayloadType route_net_to_422;

static void route_422loop(const uint8_t *payload, uint8_t length)
{
  uint8_t i;
  
  for(i=0; i<3; i++)
  {
    RS422_LinkSend(payload, length);
    
    if(xQueueReceive(Route_RxLink422_Qhd, &Route_RxLink422, 333) == pdTRUE)
    {
      if(Route_RxLink422.payload[0] != ROUTE_ROOT_SUBDEV_TO_NET)
      {
        continue;
      }
      
      if(Route_ToNetPub_Qhd != NULL)
      {
        xQueueSend(Route_ToNetPub_Qhd, &Route_RxLink422, 0);
        
        return;
      }
    }
  }
}

void vRoute_422_Task(void * pvParameters)
{
  RS422_BspInit();
  
	while(1)
	{
    if(xQueueReceive(Route_NetTo422_Qhd, &route_net_to_422, 1000) == pdTRUE)
		{
      route_422loop(route_net_to_422.payload, route_net_to_422.length);
    }
    else
    {
      route_422loop((const uint8_t*)&Route_time_block, sizeof(Route_LS_UN_TimeBlockType));
    }
	}
}
